#!/bin/bash

eros_prefix="[\033[34meros\033[0m]"

function error_exit {
    echo "--------------------------------------------"
    echo -e "\033[31m Error code is $1 \033[0m" 1>&2
    echo ""
    echo " Code tips: "
    echo -e "\033[33m1 -> platforms/ios/WeexEros/ is not existed. \033[0m"
    echo -e "\033[33m2 -> cannot remove platforms/ios/WeexEros/WeexiOSSDK, please check file permissions. \033[0m"
    # echo -e "\033[33m3 -> cannot remove platforms/ios/WeexEros/Benmu-iOS-Library, please check file permissions. \033[0m"
    echo -e "\033[33m3 -> cannot remove platforms/ios/WeexEros/ErosPluginBaseLibrary, please check file permissions. \033[0m"
    echo -e "\033[33m4 -> cannot remove platforms/ios/WeexEros/ErosPluginAmap, please check file permissions. \033[0m"
    echo -e "\033[33m5 -> cannot remove platforms/ios/WeexEros/ErosPluginGeTui, please check file permissions. \033[0m"
    echo -e "\033[33m6 -> cannot remove platforms/ios/WeexEros/ErosPluginWXPay, please check file permissions. \033[0m"
    echo -e "\033[33m7 -> cannot remove platforms/ios/WeexEros/ErosPluginWXShare, please check file permissions. \033[0m"
    echo -e "\033[33m8 -> failed run: git clone https://github.com/bmfe/WeexiOSSDK.git. \033[0m"
    # echo -e "\033[33m5 -> failed run: git clone https://github.com/bmfe/Benmu-iOS-Library.git. \033[0m"
    echo -e "\033[33m9 -> failed run: git clone https://github.com/bmfe/eros-plugin-ios-baseLibrary.git. \033[0m"
    echo -e "\033[33m10 -> failed run: git clone https://github.com/bmfe/eros-plugin-ios-amap. \033[0m"
    echo -e "\033[33m11 -> failed run: git clone https://github.com/bmfe/eros-plugin-ios-getui.git. \033[0m"
    echo -e "\033[33m12 -> failed run: git clone https://github.com/bmfe/eros-plugin-ios-wxpay. \033[0m"
    echo -e "\033[33m13 -> failed run: git clone https://github.com/bmfe/eros-plugin-ios-wxshare.git. \033[0m"
    echo -e "\033[33m14 -> pod update failed. \033[0m"
    echo -e "\033[33m15 -> run open xcode failed, make sure you had install xcode in appStore. \033[0m"
    exit 1
}

cd platforms/ios/WeexEros/ || \
error_exit 1
echo -e "$eros_prefix \033[33mtips: make sure xcode had been closed.\033[0m"
echo -e "$eros_prefix Enter ios project"

# if [ -e WeexiOSSDK/ ] || [ -e Benmu-iOS-Library/ ] || [ -e ErosPluginAmap/ ] || [ -e ErosPluginGeTui/ ] || [ -e ErosPluginWXPay/ ] || [ -e ErosPluginWXShare/ ]
if [ -e WeexiOSSDK/ ] || [ -e ErosPluginBaseLibrary/ ] || [ -e ErosPluginAmap/ ] || [ -e ErosPluginGeTui/ ] || [ -e ErosPluginWXPay/ ] || [ -e ErosPluginWXShare/ ]
then
    echo -e "$eros_prefix WeexiOSSDK and Benmu-iOS-Library and ErosPluginAmap and ErosPluginGeTui and ErosPluginWXPay and ErosPluginWXShare is existed, remove old sdk."
    rm -rf WeexiOSSDK/ || error_exit 2
    # rm -rf Benmu-iOS-Library/ || error_exit 3
    rm -rf ErosPluginBaseLibrary/ || error_exit 3
    rm -rf ErosPluginAmap/ || error_exit 4
    rm -rf ErosPluginGeTui/ || error_exit 5
    rm -rf ErosPluginWXPay/ || error_exit 6
    rm -rf ErosPluginWXShare/ || error_exit 7
else
    echo -e "$eros_prefix No WeexiOSSDK and Benmu-iOS-Library, we will start to clone new sdk..."
fi
    echo -e "$eros_prefix \033[36mMay spend a lot of time, please wait patiently. \033[0m"
    git clone https://github.com/bmfe/WeexiOSSDK.git -q --depth=1 || \
    error_exit 8
    echo -e "$eros_prefix Weex sdk clone done."
    # git clone https://github.com/bmfe/Benmu-iOS-Library.git -q --depth=1 || \
    git clone https://github.com/bmfe/eros-plugin-ios-baseLibrary.git -q --depth=1 || \
    error_exit 9
    echo -e "$eros_prefix Eros sdk clone done." 
    git clone https://github.com/bmfe/eros-plugin-ios-amap -q --depth=1 || \
    error_exit 10
    echo -e "$eros_prefix Eros amap clone done." 
    git clone https://github.com/bmfe/eros-plugin-ios-getui.git -q --depth=1 || \
    error_exit 11
    echo -e "$eros_prefix Eros getui clone done." 
    git clone https://github.com/bmfe/eros-plugin-ios-wxpay -q --depth=1 || \
    error_exit 12
    echo -e "$eros_prefix Eros wxpay clone done." 
    git clone https://github.com/bmfe/eros-plugin-ios-wxshare.git -q --depth=1 || \
    error_exit 13
    echo -e "$eros_prefix Eros umeng clone done." 
    echo -e "$eros_prefix Run pod update and show some update detail:"
    pod update --no-ansi || error_exit 14
    echo ""
    echo -e "$eros_prefix \033[32miOS sdk has been installed, enjoy it! \033[0m"
    open WeexEros.xcworkspace || error_exit 15
